package cn.com.blueInfo.bpm.template.service.impl;

import cn.com.blueInfo.bpm.template.convert.ViewConvert;
import cn.com.blueInfo.bpm.template.entity.View;
import cn.com.blueInfo.bpm.template.repository.ViewRepository;
import cn.com.blueInfo.bpm.template.request.ViewDTO;
import cn.com.blueInfo.bpm.template.response.ViewVO;
import cn.com.blueInfo.bpm.template.service.ViewService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 流程图配置表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class ViewServiceImpl implements ViewService {

    @Autowired
    private ViewRepository viewRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private ViewConvert viewConvert;

    /**
     * 新增流程图配置
     * @param viewDTO 流程图配置参数
     */
    @Override
    public ResultInfo<Integer> addView(ViewDTO viewDTO) {
        View view = viewConvert.dtoToEntity(viewDTO);
        view.setId(snowflakeIdGenerator.nextId());
        Integer addSize = viewRepository.createView(view);
        return ResultInfo.<Integer>builder().data(addSize).message("流程图配置成功新增 {} 条").build();
    }

    /**
     * 批量新增流程图配置
     * @param viewDTOList 流程图配置参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddView(List<ViewDTO> viewDTOList) {
        List<View> viewList = viewConvert.dtoToEntity(viewDTOList);
        for (View view : viewList) view.setId(snowflakeIdGenerator.nextId());
        Integer addSize = viewRepository.batchCreateView(viewList);
        return ResultInfo.<Integer>builder().data(addSize).message("流程图配置成功新增 {} 条").build();
    }

    /**
     * 删除流程图配置
     * @param viewId 流程图配置ID
     */
    @Override
    public ResultInfo<Integer> removeView(Long viewId) {
        Integer removeSize = viewRepository.deleteView(viewId);
        return ResultInfo.<Integer>builder().data(removeSize).message("流程图配置成功删除 {} 条").build();
    }

    /**
     * 批量删除流程图配置
     * @param viewIdList 流程图配置ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveView(List<Long> viewIdList) {
        Integer removeSize = viewRepository.batchDeleteView(viewIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("流程图配置成功删除 {} 条").build();
    }

    /**
     * 更新部分流程图配置
     * @param viewUpdatePartialDTO 流程图配置参数
     */
    @Override
    public ResultInfo<Integer> modifyViewPartial(ViewDTO.UpdatePartialDTO viewUpdatePartialDTO) {
        View view = viewConvert.dtoToEntity(viewUpdatePartialDTO);
        Integer modifySize = viewRepository.updateViewPartial(view);
        return ResultInfo.<Integer>builder().data(modifySize).message("流程图配置成功更新 {} 条").build();
    }

    /**
     * 更新全部流程图配置
     * @param viewDTO 流程图配置参数
     */
    @Override
    public ResultInfo<Integer> modifyView(ViewDTO viewDTO) {
        View view = viewConvert.dtoToEntity(viewDTO);
        Integer modifySize = viewRepository.updateView(view);
        return ResultInfo.<Integer>builder().data(modifySize).message("流程图配置成功更新 {} 条").build();
    }

    /**
     * 查询一条流程图配置
     * @param viewId 流程图配置ID
     */
    @Override
    public ResultInfo<ViewVO> queryViewById(Long viewId) {
        View view = viewRepository.getViewById(viewId);
        ViewVO viewVO = viewConvert.entityToVO(view);
        return ResultInfo.<ViewVO>builder().data(viewVO).message("流程图配置查询成功").build();
    }

    /**
     * 分页查询流程图配置
     * @param viewQueryDTO 流程图配置参数
     */
    @Override
    public ResultInfo<Page<ViewVO>> queryViewPage(ViewDTO.QueryDTO viewQueryDTO) {
        Page<View> viewPage = viewRepository.getViewPage(viewQueryDTO);
        Page<ViewVO> resultData = viewConvert.convertPage(viewPage);
        return ResultInfo.<Page<ViewVO>>builder()
                .data(resultData)
                .message("流程图配置分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 流程图配置 数量
     * @param viewQueryDTO 流程图配置参数
     */
    @Override
    public ResultInfo<Long> queryViewCount(ViewDTO.QueryDTO viewQueryDTO) {
        Long resultCount = viewRepository.getViewCount(viewQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("流程图配置数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 流程图配置
     * @param viewQueryDTO 流程图配置参数
     */
    @Override
    public ResultInfo<List<ViewVO>> queryViewList(ViewDTO.QueryDTO viewQueryDTO) {
        List<View> viewList = viewRepository.getViewList(viewQueryDTO);
        List<ViewVO> resultData = viewConvert.entityListToVoList(viewList);
        return ResultInfo.<List<ViewVO>>builder()
                .data(resultData)
                .message("流程图配置分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
